package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;




    /*
    * 新增套餐
    * */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }



    /*
    * 页面查询
    * */
    @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,String name){
        //分页构造器
        Page pageInfo = new Page<>(page,pageSize);


        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null,Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,lambdaQueryWrapper);

        //获取套餐分类数据
        Page<SetmealDto> pageSetmealDto = new Page<>();
        BeanUtils.copyProperties(pageInfo,pageSetmealDto,"records");
        List<Setmeal> setmeals = pageInfo.getRecords();
        List<SetmealDto> collect = setmeals.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;


        }).collect(Collectors.toList());
        pageSetmealDto.setRecords(collect);

        return R.success(pageSetmealDto);

    }



    /*
    * 删除套餐
    * */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        setmealService.removeWithDish(ids);

        return R.success("套餐数据删除成功");

    }


    /*
     * 状态更新
     * */
    @PostMapping("/status/{status}")
    public R<String> SetmealStatusByStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        //更新条件
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //更新对象
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        //更新操作
        setmealService.update(setmeal,lambdaQueryWrapper);
        return R.success("更新成功");
    }




    /*
     * 根据id查询套餐信息和对应的口味信息
     * */
    @GetMapping("/{id}")
    public R<SetmealDto> getId(@PathVariable Long id){

        log.info("需要查询的套餐id:{}",id);

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);


        return R.success(setmealDto);
    }



    /*
     * 修改套餐
     *
     * */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());

        setmealService.updateWithDish(setmealDto);


        return R.success("修改成功");
    }


    /*
    * 根据条件查询套餐数据
    * */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getCategoryId()!=null, Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);

        return R.success(list);
    }

}
